package ee.helmesWorkTest.backend.model.dao.postgresqlimplementations;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.apache.log4j.Logger;
import ee.helmesWorkTest.backend.database.ConnectionManager;
import ee.helmesWorkTest.backend.model.dao.PersonSectorInvolvementDAO;
import ee.helmesWorkTest.backend.model.data.PersonSectorInvolvement;

public class PersonSectorInvolvementDAOPostgresqlImpl implements PersonSectorInvolvementDAO
{
	Logger logger = Logger.getLogger(PersonSectorInvolvementDAOPostgresqlImpl.class);
	Connection dbConnection;
	String sql;
	PreparedStatement preparedStatement;
	Statement statement;
	ResultSet resultSet;
	private final int WARNING_NO_ROWS_AFFECTED = 0;
	private int rowsAffected;
	
	public PersonSectorInvolvementDAOPostgresqlImpl(Connection dbConnection)
	{
		this.dbConnection = dbConnection;
	}
	
	@Override
	public int insertPersonSectorInvolvement(
			PersonSectorInvolvement personSectorInvolvement)
	{
		resultSet = null;
		rowsAffected = WARNING_NO_ROWS_AFFECTED;
		sql = "INSERT INTO Person_sectors_involvement " +  
				"(person_fk, sector_fk, agrees_to_terms) " +   
				"VALUES " + 
				"(?, ?, ?);";
		
		try 
		{
			preparedStatement = dbConnection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
			
			preparedStatement.setInt	(1, personSectorInvolvement.getPersonFK());
			preparedStatement.setInt	(2, personSectorInvolvement.getSectorFK());
			preparedStatement.setBoolean(3, personSectorInvolvement.getAgreesToTerms());
			
			logger.info(preparedStatement.toString());
			rowsAffected = preparedStatement.executeUpdate();
			
			if (rowsAffected == 1)
			{
				resultSet = preparedStatement.getGeneratedKeys();
				resultSet.next();
				int newId = resultSet.getInt(1);
				personSectorInvolvement.setPersonSectorInvolvementID(newId);
				return rowsAffected;
			}
			else
			{
				System.out.println("No rows affected!");
				logger.info("Unsuccessful insertion to a database: " + preparedStatement.toString());
				return WARNING_NO_ROWS_AFFECTED;
			}
		}
		catch (Exception e) 
		{
			e.printStackTrace();
			return WARNING_NO_ROWS_AFFECTED;
		}
		finally
		{
			ConnectionManager.getInstance().closeStatement(preparedStatement);
			ConnectionManager.getInstance().closeResultSet(resultSet);
		}
	}

	@Override
	public PersonSectorInvolvement getPersonSectorInvolvementByID(
			int personSectorInvolvementID) 
	{
		resultSet = null;
		PersonSectorInvolvement personSectorInvolvementRetrieved = null;
		sql = "SELECT * FROM Person_sectors_involvement WHERE person_sector_involvement_id = ?;";
		
		try 
		{
			preparedStatement = dbConnection.prepareStatement(
						sql,
						ResultSet.TYPE_SCROLL_INSENSITIVE,
						ResultSet.CONCUR_READ_ONLY
					);
			preparedStatement.setInt(1, personSectorInvolvementID);
			logger.info(preparedStatement.toString());
			resultSet = preparedStatement.executeQuery();
			resultSet.beforeFirst();
			
			if (resultSet.next())
			{
				personSectorInvolvementRetrieved = new PersonSectorInvolvement();
				personSectorInvolvementRetrieved.setPersonFK(resultSet.getInt("person_fk"));
				personSectorInvolvementRetrieved.setSectorFK(resultSet.getInt("sector_fk"));
				personSectorInvolvementRetrieved.setAgreesToTerms(resultSet.getBoolean("agrees_to_terms"));
				personSectorInvolvementRetrieved.setPersonSectorInvolvementID(resultSet.getInt("person_sector_involvement_id"));
			}
		}
		catch (SQLException e) 
		{
			logger.error(e.getMessage());
		}
		finally
		{
			ConnectionManager.getInstance().closeStatement(preparedStatement);
			ConnectionManager.getInstance().closeResultSet(resultSet);
		}
		
		return personSectorInvolvementRetrieved;
	}

	@Override
	public int updatePersonSectorInvolvement(
			PersonSectorInvolvement personSectorInvolvement)
	{
		resultSet = null;
		rowsAffected = WARNING_NO_ROWS_AFFECTED;
		sql = "UPDATE Person_sectors_involvement " +  
				"SET person_fk = ?, sector_fk = ?, agrees_to_terms = ? " +   
				"WHERE person_sector_involvement_id = ? ";
		
		try 
		{
			preparedStatement = dbConnection.prepareStatement(sql);
			
			preparedStatement.setInt	(1, personSectorInvolvement.getPersonFK());
			preparedStatement.setInt	(2, personSectorInvolvement.getSectorFK());
			preparedStatement.setBoolean(3, personSectorInvolvement.getAgreesToTerms());
			preparedStatement.setInt	(4, personSectorInvolvement.getPersonSectorInvolvementID());
			
			rowsAffected = preparedStatement.executeUpdate();
			
			if (rowsAffected == 1)
			{
				logger.info("Update sucessful: " + preparedStatement.toString());
				return rowsAffected;
			}
			else
			{
				logger.error("Update was unsuccesful: " + preparedStatement.toString());
				return WARNING_NO_ROWS_AFFECTED;
			}
		}
		catch (SQLException e) 
		{
			logger.error(e.getMessage());
			return WARNING_NO_ROWS_AFFECTED;		
		}
		finally
		{
			ConnectionManager.getInstance().closeStatement(preparedStatement);
			ConnectionManager.getInstance().closeResultSet(resultSet);
		}
	}
	
	@Override
	public int deletePersonSectorInvolvement(int personID)
	{
		resultSet = null;
		rowsAffected = WARNING_NO_ROWS_AFFECTED;
		sql = "DELETE FROM Person_sectors_involvement " + 
				"WHERE person_fk = ?;";
		
		try 
		{
			preparedStatement = dbConnection.prepareStatement(sql);
			
			preparedStatement.setInt(1, personID);
			
			rowsAffected = preparedStatement.executeUpdate();

		}
		catch (SQLException e) 
		{
			e.printStackTrace();
			return WARNING_NO_ROWS_AFFECTED;		
		}
		finally
		{
			ConnectionManager.getInstance().closeStatement(preparedStatement);
			ConnectionManager.getInstance().closeResultSet(resultSet);
		}
		
		return 1;
	}
}
